﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DefCore.Interfaces;
using Microsoft.Xna.Framework.Graphics;

namespace DefCore.Actors
{
    public sealed class HealthbarService
    {
        private static HealthbarService _Instance;
        public static HealthbarService Instance
        {
            get
            {
                if (_Instance == null)
                    _Instance = new HealthbarService();

                return _Instance;
            }
        }

        //Fields        
        private const int _HealthbarWidth = 64;
        private const int _HealthbarHeight = 32;

        private RenderTarget2D _RT;
        private Dictionary<KeyValuePair<int, int>, Texture2D> _HealthbarCache;

        //ctor
        protected HealthbarService()
        {
            _RT = null;
            _HealthbarCache = new Dictionary<KeyValuePair<int, int>, Texture2D>();
        }
        
        //Methods
        private bool HasHealthBarInCache(KeyValuePair<int, int> currMaxHealth)
        {
            return _HealthbarCache.ContainsKey(currMaxHealth);
        }

        public Texture2D GenerateHealthbar(IHealthController healthControl)
        {
            if(HasHealthBarInCache(new KeyValuePair<int,int>(healthControl.CurrentHealth, healthControl.MaxHealth)))
                return _HealthbarCache[new KeyValuePair<int,int>(healthControl.CurrentHealth, healthControl.MaxHealth)];


            _RT = new RenderTarget2D(Objects.GameObject.GraphicsDevice, _HealthbarWidth, _HealthbarHeight, false, SurfaceFormat.Color, DepthFormat.None);
            using (SpriteBatch b = new SpriteBatch(Objects.GameObject.GraphicsDevice))
            {
                b.Begin();



                b.End();
            }

            _HealthbarCache.Add(new KeyValuePair<int,int>(healthControl.CurrentHealth, healthControl.MaxHealth), _RT);
            return _RT;
        }
    }
}
